package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.message.*;
import com.mathlearning.auth.service.MessageService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取会话列表
     */
    @GetMapping("/conversations")
    public ApiResponse<ConversationListResponse> getConversations(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        ConversationListResponse response = messageService.getConversations(currentUserId, page, pageSize);
        return ApiResponse.success(response);
    }

    /**
     * 获取与某个用户的聊天记录
     */
    @GetMapping("/user/{userId}")
    public ApiResponse<MessageListResponse> getMessagesWithUser(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "50") Integer pageSize,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        MessageListResponse response = messageService.getMessagesWith(currentUserId, userId, page, pageSize);
        return ApiResponse.success(response);
    }

    /**
     * 发送私信
     */
    @PostMapping("/send")
    public ApiResponse<MessageResponse> sendMessage(
            @Valid @RequestBody SendMessageRequest sendRequest,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        MessageResponse response = messageService.sendMessage(currentUserId, sendRequest);
        return ApiResponse.success("发送成功", response);
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/mark-read/{userId}")
    public ApiResponse<MarkReadResponse> markAsRead(
            @PathVariable Long userId,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        MarkReadResponse response = messageService.markMessagesAsRead(currentUserId, userId);
        return ApiResponse.success("标记成功", response);
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread-count")
    public ApiResponse<UnreadCountResponse> getUnreadCount(HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        UnreadCountResponse response = messageService.getUnreadCount(currentUserId);
        return ApiResponse.success(response);
    }

    /**
     * 删除会话（可选）
     */
    @DeleteMapping("/conversation/{userId}")
    public ApiResponse<?> deleteConversation(
            @PathVariable Long userId,
            HttpServletRequest request) {
        Long currentUserId = (Long) request.getAttribute("userId");
        messageService.deleteConversation(currentUserId, userId);
        return ApiResponse.success("删除成功");
    }
}








